﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Drawing;
using Newtonsoft.Json;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Drawing.Imaging;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace NCHQMES
{
    public class Method
    {
        #region 全局字段

        /// <summary>
        /// 程序启动位置
        /// </summary>
        public static string DebugPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Config" + "\\";

        #endregion 全局字段

        #region MES访问方法

        /// <summary>
        /// 访问MES
        /// </summary>
        /// <param name="strData">访问字符串</param>
        /// <returns>MES返回字符串</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static string AccessAWebsiteByHttp(string strData)
        {
            string strR = string.Empty;
            //try
            //{
                MachineInfo machineInfo = JsonToObject<MachineInfo>(ReadPath<MachineInfo>());

                if (machineInfo == null && string.IsNullOrEmpty(strData))
                {
                    return strR;
                }

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(machineInfo.MesUrl));

                byte[] buf = System.Text.Encoding.UTF8.GetBytes(strData);
                request.Method = "POST";
                request.ContentLength = 0;
                request.ContentType = "application/json";
                request.Timeout = machineInfo.TimeOut;
                request.ContentLength = buf.Length;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(buf, 0, buf.Length);
                    stream.Close();
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        strR = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
                        reader.Close();
                        response.Close();
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    AppendMesInfoToRichTextBoxLog("方法:访问MES{ public static string AccessAWebsiteByHttp(string strData)}出错,错误信息:" + ex.Message, Color.Red);
            //}

            return strR;
        }

        /// <summary>
        /// MES心跳发送
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string MES_SCADA_Heartbeat()
        {
            StringBuilder builder = new StringBuilder();
            //try
            //{
                string str;
                MachineInfo Mache = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());
                if (Mache == null)
                {
                    return "Mache == null";
                }

                SCADA_Heartbeat.Write Mes = new SCADA_Heartbeat.Write();
                Mes.HEAD.H_OP = Mache.SCADA_Heartbeat;
                Mes.HEAD.H_GUID = Mache.H_GUID;
                Mes.HEAD.H_TOKEN = Mache.H_TOKEN;
                Mes.HEAD.H_SRC_SYS = Mache.H_SRC_SYS;
                Mes.MAIN.EQSN = Mache.EQSN;
                str = Method.ObjectToJson(Mes);
                builder.AppendLine("发送{心跳}到MES:" + str);
                str = Method.AccessAWebsiteByHttp(str);
                builder.AppendLine("接收MES{心跳}数据:" + str);
                SCADA_Heartbeat.Read read = Method.JsonToObject<SCADA_Heartbeat.Read>(str);
                if (read.HEAD.H_RET == "00001")
                {
                    builder.AppendLine("发送{心跳}到MES{成功}");
                }
                else
                {
                    builder.AppendLine("发送{心跳}到MES{失败}:,失败信息:" + read.HEAD.H_MSG);
                }
            //}
            //catch (Exception ex)
            //{
            //    AppendMesInfoToRichTextBoxLog("MES心跳出错,错误信息:" + ex.Message, Color.Red);
            //}
            return builder.ToString();
        }

        /// <summary>
        ///软件打开时调用
        /// </summary>
        /// <param name="OperatorName">作业员</param>
        /// <returns></returns>
        /// <exception cref="Exception">错误信息</exception>
        public static string MES_SCADA_Startup(string OperatorName)
        {
            StringBuilder builder = new StringBuilder();
            //try
            //{
                string str;
                MachineInfo Mache = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());

                if (Mache == null)
                {
                    return "Mache == null";
                }
                SCADA_Startup.Write Mes = new SCADA_Startup.Write();
                Mes.HEAD.H_OP = Mache.SCADA_Startup;
                Mes.HEAD.H_GUID = Mache.H_GUID;
                Mes.HEAD.H_TOKEN = Mache.H_TOKEN;
                Mes.HEAD.H_SRC_SYS = Mache.H_SRC_SYS;
                Mes.MAIN.EQSN = Mache.EQSN;
                Mes.MAIN.SoftwareName = Mache.SoftwareName;
                Mes.MAIN.OperatorName = OperatorName;
                Mes.MAIN.HostName = Mache.HostName;
                Mes.MAIN.IP = Mache.IP;
                Mes.MAIN.SoftwareStartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                str = Method.ObjectToJson(Mes);
                builder.AppendLine("发送{软件打开}到MES:" + str);
                str = Method.AccessAWebsiteByHttp(str);
                builder.AppendLine("接收MES{软件打开}数据:" + str);
                SCADA_Startup.Read read = Method.JsonToObject<SCADA_Startup.Read>(str);
                if (read.HEAD.H_RET == "00001")
                {
                    builder.AppendLine("发送{软件打开}到MES{成功}");
                }
                else
                {
                    builder.AppendLine("发送{软件打开}到MES{失败}:,失败信息:" + read.HEAD.H_MSG);
                }
            //}
            //catch (Exception ex)
            //{
            //    AppendMesInfoToRichTextBoxLog("软件打开时出错,错误信息:" + ex.Message, Color.Red);
            //}
            return builder.ToString();
        }

        /// <summary>
        /// 报警产生与消除
        /// </summary>
        /// <param name="errType">报警类型</param>
        /// <param name="errCode">报警代码</param>
        /// <param name="OperatorName">作业员</param>
        /// <returns></returns>
        /// <exception cref="Exception">错误信息</exception>
        public static string MES_SCADA_Warning(string errType, string errCode, string OperatorName)
        {
            StringBuilder builder = new StringBuilder();
            //try
            //{
                string str;
                MachineInfo Mache = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());
                ErrClass Err = Method.FindInErrClassByerrCode(errCode);
                if (Mache == null || Err == null)
                {
                    return "Mache == null || Err == null";
                }

                SCADA_Warning.Write Mes = new SCADA_Warning.Write();
                Mes.HEAD.H_OP = Mache.SCADA_Warning;
                Mes.HEAD.H_GUID = Mache.H_GUID;
                Mes.HEAD.H_TOKEN = Mache.H_TOKEN;
                Mes.HEAD.H_SRC_SYS = Mache.H_SRC_SYS;
                Mes.MAIN.EQSN = Mache.EQSN;
                Mes.MAIN.OperatorName = OperatorName;
                Mes.MAIN.HostName = Mache.HostName;
                Mes.MAIN.IP = Mache.IP;
                Mes.MAIN.Type = errType;
                Mes.MAIN.Code = Err.MesCode;

                Mes.MAIN.Descr = Err.ErrMsg;
                Mes.MAIN.Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                str = Method.ObjectToJson(Mes);
                builder.AppendLine("发送{报警信息}到MES:" + str);
                str = Method.AccessAWebsiteByHttp(str);
                builder.AppendLine("接收MES{报警信息}数据:" + str);
                SCADA_Warning.Read read = Method.JsonToObject<SCADA_Warning.Read>(str);
                if (read.HEAD.H_RET == "00001")
                {
                    builder.AppendLine("发送{报警信息}到MES{成功}");
                }
                else
                {
                    builder.AppendLine("发送{报警信息}到MES{失败}:,失败信息:" + read.HEAD.H_MSG);
                }
            //}
            //catch (Exception ex)
            //{
            //    AppendMesInfoToRichTextBoxLog("报警产生与消除出错,错误信息:" + ex.Message, Color.Red);
            //}
            return builder.ToString();
        }

        /// <summary>
        /// 设备状态改变
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="OperatorName">作业员</param>
        /// <returns></returns>
        /// <exception cref="Exception">错误信息/exception>
        public static string MES_SCADA_StatusChange(string status, string OperatorName)
        {
            StringBuilder builder = new StringBuilder();
            //try
            //{
                string str;
                MachineInfo Mache = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());
                if (Mache == null)
                {
                    return "Mache == null";
                }

                SCADA_StatusChange.Write Mes = new SCADA_StatusChange.Write();
                Mes.HEAD.H_OP = Mache.SCADA_StatusChange;
                Mes.HEAD.H_GUID = Mache.H_GUID;
                Mes.HEAD.H_TOKEN = Mache.H_TOKEN;
                Mes.HEAD.H_SRC_SYS = Mache.H_SRC_SYS;
                Mes.MAIN.EQSN = Mache.EQSN;
                Mes.MAIN.OperatorName = OperatorName;
                Mes.MAIN.HostName = Mache.HostName;
                Mes.MAIN.IP = Mache.IP;
                Mes.MAIN.Status = status.Trim();
                str = Method.ObjectToJson(Mes);
                builder.AppendLine("发送{设备状态}到MES:" + str);
                str = Method.AccessAWebsiteByHttp(str);
                builder.AppendLine("接收MES{设备状态}数据:" + str);
                SCADA_StatusChange.Read read = Method.JsonToObject<SCADA_StatusChange.Read>(str);
                if (read.HEAD.H_RET == "00001")
                {
                    builder.AppendLine("发送{设备状态}到MES{成功}");
                }
                else
                {
                    builder.AppendLine("发送{设备状态}到MES{失败}:,失败信息:" + read.HEAD.H_MSG);
                }
            //}
            //catch (Exception ex)
            //{
            //    AppendMesInfoToRichTextBoxLog("设备状态改变出错,错误信息:" + ex.Message, Color.Red);
            //}
            return builder.ToString();
        }

        /// <summary>
        /// 锁附完成数据上传
        /// </summary>
        /// <param name="MesScrewWorkData">工作接口实体类</param>
        /// <returns></returns>
        /// <exception cref="Exception">错误信息</exception>
        public static string MES_SCADA_ScrewWorkData(SCADA_ScrewWorkData.Write MesScrewWorkData)
        {
            StringBuilder builder = new StringBuilder();
            //try
            //{
                string str;
                MachineInfo Mache = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());
                if (Mache == null)
                {
                    return "Mache == null";
                }

                MesScrewWorkData.HEAD.H_OP = Mache.SCADA_ScrewWorkData;
                MesScrewWorkData.HEAD.H_GUID = Mache.H_GUID;
                MesScrewWorkData.HEAD.H_TOKEN = Mache.H_TOKEN;
                MesScrewWorkData.HEAD.H_SRC_SYS = Mache.H_SRC_SYS;

                MesScrewWorkData.MAIN.BU = Mache.BU;
                MesScrewWorkData.MAIN.Dept = Mache.Dept;
                MesScrewWorkData.MAIN.Line = Mache.Line;
                MesScrewWorkData.MAIN.EQSN = Mache.EQSN;
                MesScrewWorkData.MAIN.Station = Mache.Station;
                MesScrewWorkData.MAIN.IP = Mache.IP;
                MesScrewWorkData.MAIN.HostName = Mache.HostName;
                MesScrewWorkData.MAIN.SN = "NOREAD";
                MesScrewWorkData.MAIN.SetHeadPosition = "A";

                MesScrewWorkData.MAIN.Pressure = "0.6";
                MesScrewWorkData.MAIN.AssistCheck = "";
                MesScrewWorkData.MAIN.AssistCheck = "0";

                str = Method.ObjectToJson(MesScrewWorkData);
                builder.AppendLine("发送{锁附完成}到MES:" + str);
                str = Method.AccessAWebsiteByHttp(str);
                builder.AppendLine("接收MES{锁附完成}数据:" + str);
                SCADA_ScrewWorkData.Read read = Method.JsonToObject<SCADA_ScrewWorkData.Read>(str);
                if (read.HEAD.H_RET == "00001")
                {
                    builder.AppendLine("发送{锁附完成}到MES{成功}");
                }
                else
                {
                    builder.AppendLine("发送{锁附完成}到MES{失败}:,失败信息:" + read.HEAD.H_MSG);
                }
            //}
            //catch (Exception ex)
            //{
            //    AppendMesInfoToRichTextBoxLog("锁附完成数据上传出错,错误信息:" + ex.Message, Color.Red);
            //}
            return builder.ToString();
        }

        /// <summary>
        /// 测试接口
        /// </summary>
        public static string MES_SCADA_FormateDemo()
        {
            StringBuilder builder = new StringBuilder();
            //try
            //{
                string str;
                MachineInfo Mache = Method.JsonToObject<MachineInfo>(Method.ReadPath<MachineInfo>());
                if (Mache != null)
                {
                    SCADA_FormateDemo.Write Mes = new SCADA_FormateDemo.Write();
                    Mes.HEAD.H_OP = Mache.SCADA_FormateDemo;
                    Mes.HEAD.H_GUID = Mache.H_GUID;
                    Mes.HEAD.H_TOKEN = Mache.H_TOKEN;
                    Mes.HEAD.H_SRC_SYS = Mache.H_SRC_SYS;
                    Mes.MAIN.EQSN = Mache.EQSN;
                    Mes.MAIN.OperatorName = "";
                    Mes.MAIN.HostName = Mache.HostName;
                    Mes.MAIN.IP = Mache.IP;
                    Mes.MAIN.TESTTYPE = "PASS";
                    Mes.MAIN.SN = "NOREAD";
                    str = Method.ObjectToJson(Mes);
                    builder.AppendLine("发送{测试接口}到MES:" + str);
                    str = Method.AccessAWebsiteByHttp(str);
                    builder.AppendLine("接收MES{测试接口}数据:" + str);
                    SCADA_FormateDemo.Read read = Method.JsonToObject<SCADA_FormateDemo.Read>(str);
                    if (read.HEAD.H_RET == "00001")
                    {
                        builder.AppendLine("发送{测试接口}到MES{成功}");
                    }
                    else
                    {
                        builder.AppendLine("发送{测试接口}到MES{失败}:,失败信息:" + read.HEAD.H_MSG);
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    AppendMesInfoToRichTextBoxLog("方法:测试接口出错,错误信息:" + ex.Message, Color.Red);
            //}
            return builder.ToString();
        }

        #endregion MES访问方法

        #region 其它方法

        /// <summary>
        /// JSON字符串序列化为实体类
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="JsonStr">字符串</param>
        /// <returns>实体类</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static T JsonToObject<T>(string JsonStr)
        {
            if (string.IsNullOrEmpty(JsonStr))
            {
                return default(T);
            }

            return JsonConvert.DeserializeObject<T>(JsonStr);
        }

        /// <summary>
        /// 实体类转换为Json字符串
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="objT">实体类</param>
        /// <returns>Json字符串</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static string ObjectToJson<T>(T objT)
        {
            if (objT == null)
            {
                return "";
            }
            return JsonConvert.SerializeObject(objT);
        }

        /// <summary>
        /// List集合序列化Json
        /// </summary>
        /// <param name="objT">实体类</param>
        /// <returns>字符串</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static string ListToJson(object objT)
        {
            if (objT == null)
            {
                return "";
            }
            return JsonConvert.SerializeObject(objT);
        }

        /// <summary>
        /// Json字符串转换为List集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="JsonStr">Json字符串</param>
        /// <returns>list集合</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static List<T> JsonToList<T>(string JsonStr)
        {
            if (string.IsNullOrEmpty(JsonStr))
            {
                return default(List<T>);
            }
            return JsonConvert.DeserializeObject<List<T>>(JsonStr);
        }

        /// <summary>
        /// 读取文本
        /// </summary>
        /// <param name="name">文本名称</param>
        /// <returns>返回读取字符串</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static string ReadPath<T>()
        {
            string str = string.Empty;
 
                string path = DebugPath + typeof(T).Name + ".json";
                if (File.Exists(path))
                {
                    using (FileStream Fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        using (StreamReader Sr = new StreamReader(Fs))
                        {
                            str = Sr.ReadLine();
                            str = str.Trim();
                            Sr.Close();
                            Fs.Close();
                        }
                    }
                }
  
            return str;
        }

        /// <summary>
        /// 写入字符串文本
        /// </summary>
        /// <param name="name">文本名</param>
        /// <param name="str">字符串</param>
        /// <returns>true:写入成功；false:写入失败</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static bool WritePathTxt<T>(string str)
        {

                string path = DebugPath + typeof(T).Name + ".json";
                if (!Directory.Exists(DebugPath))
                {
                    Directory.CreateDirectory(DebugPath);
                }
                using (FileStream Fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    using (StreamWriter Sw = new StreamWriter(Fs))
                    {
                        Sw.Write(str);
                        Sw.Close();
                        Fs.Close();
                    }
                }
            

            return true;
        }

        /// <summary>
        /// 写入文本
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static bool WritePathTxt(string str)
        {

                string path = DebugPath + "SCADA_Heartbeat.txt";
                if (!Directory.Exists(DebugPath))
                {
                    Directory.CreateDirectory(DebugPath);
                }

                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(str);
                        sw.Close();
                        fs.Close();
                    }
                }
 
            return true;
        }

        /// <summary>
        /// 写入文本
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static bool WritePathTxt(string path, string pathName, string WriteStr)
        {

                string path1 = path + pathName;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                using (FileStream fs = new FileStream(path1, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(WriteStr);
                        sw.Close();
                        fs.Close();
                    }
                }

            return true;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        /// <exception cref="Exception"></exception>
        public static void DeletePath(string path)
        {

                if (Directory.Exists(path))
                {
                    DirectoryInfo di = new DirectoryInfo(path);
                    FileInfo[] files = di.GetFiles("*.txt");
                    foreach (FileInfo file in files)
                    {
                        string[] str = file.Name.Split(new char[] { '_' });
                        str[2] = str[2].Replace(".txt", " ");
                        string timestr = DateTime.Now.Date.ToString("yyyyMMdd");
                        if (Convert.ToInt32(timestr) - Convert.ToInt32(str[2]) > 10)
                        {
                            file.Delete();
                        }
                    }
                }

        }

        /// <summary>
        /// 根据报警代码查找MES报警代码
        /// </summary>
        /// <param name="errCode">报警代码</param>
        /// <returns>ErrClass实体类</returns>
        /// <exception cref="Exception">错误信息</exception>
        public static ErrClass FindInErrClassByerrCode(string errCode)
        {
            ErrClass errClass = new ErrClass("", "", "");

                List<ErrClass> errList = Method.JsonToList<ErrClass>(Method.ReadPath<ErrClass>());
                if (errList != null)
                {
                    errClass = errList.Where(c => c.Code == errCode).First();
                }

            return errClass;
        }

        #endregion 其它方法

        //#region 加密狗验证

        //#region 加密狗参数

        //private static string[] ProducId = new[] { "2BD6784B" };

        //private static string[] AdminPIN = new[] { "3FDFABADB1CDF5A2" };
        //private static string[] UserPIN = new[] { "13D3F3CE" };

        //public class JDKEY_DEFINE
        //{
        //    public const uint DONGLE_SUCCESS = 0;        // 操作成功
        //    public const uint DONGLE_NOT_FOUND = 2147483649;        // 未找到指定的加密机
        //    public const uint DONGLE_NOT_OPEN = 2147483650;        // 尚未打开加密机
        //    public const uint DONGLE_INVALID_PARAMETER = 2147483651;        // 参数错误
        //    public const uint DONGLE_FAILED = 2147483652;        // 操作失败
        //    public const uint DONGLE_NOT_SUPPORT = 2147483653;        // 操作不支持
        //    public const uint DONGLE_RUN_APP_ERROR = 2147483654;        // 运行APP错误
        //    public const uint DONGLE_SESSIONKEY_FAILED = 2147483655;        // 协商通信密钥失败
        //    public const uint DONGLE_SESSIONKEY_NOT_GEN = 2147483656;        // 尚未协商通信密钥
        //    public const uint DONGLE_SESSIONKEY_FULL = 2147483657;        // 会话密钥己满
        //    public const uint DONGLE_ADMINPIN_NOT_CHECK = 2147483658;     // 管理员密码没有验证
        //    public const uint DONGLE_USERPIN_NOT_CHECK = 2147483659;        // 用户密码没有验证
        //    public const uint DONGLE_PIN_BLOCKED = 2147483660;        // PIN码已锁死
        //    public const uint DONGLE_INCORRECT_PIN = 2147483661;        // PIN码错误
        //    public const uint DONGLE_FILE_EXIST = 2147483662;        // 文件已存在
        //    public const uint DONGLE_FILE_NOT_FOUND = 2147483663;        // 未找到指定的文件
        //    public const uint DONGLE_FILE_READ_ERROR = 2147483664;        // 读取文件错误
        //    public const uint DONGLE_FILE_WRITE_ERROR = 2147483665;        // 写入文件错误
        //    public const uint DONGLE_COMM_ERROR = 2147483666;        // 通讯错误
        //    public const uint DONGLE_ERROR_UNKNOWN = 4294967295;        // 未知的错误
        //}

        //[StructLayout(LayoutKind.Sequential)]
        //public struct DONGLE_INFO
        //{
        //    public uint bits;
        //    public uint modulus;

        //    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        //    public byte[] publicExponent;

        //    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        //    public byte[] exponent;
        //}

        //private const string DLLName = "LNLON.dll";

        //[DllImport(DLLName)]
        //public static extern int JDKey_Enum(ref DONGLE_INFO pDongleInfo, ref int pCount);

        //[DllImport(DLLName)]
        //public static extern uint JDKey_Open(ref uint phDongle, string pPID, int index);

        //[DllImport(DLLName)]
        //public static extern uint JDKey_Close(uint hDongle);

        //#endregion 加密狗参数

        ///// <summary>
        ///// 检测指定的加密狗是否存在
        ///// </summary>
        ///// <returns></returns>
        //public static bool CheckDogExistByProductId()
        //{
        //    bool flag = false;
        //    try
        //    {
        //        uint iret = 0;
        //        uint hDongle = 0;
        //        for (int i = 0; i <= ProducId.Length - 1; i++)
        //        {
        //            iret = JDKey_Open(ref hDongle, ProducId[i], 0);
        //            if (iret == JDKEY_DEFINE.DONGLE_SUCCESS)
        //            {
        //                iret = JDKey_Close(hDongle);
        //                if (iret == JDKEY_DEFINE.DONGLE_SUCCESS)
        //                {
        //                    flag = true;
        //                    break;
        //                }
        //            }
        //            else
        //            {
        //                MessageBox.Show("请插入指定加密狗");
        //                Process.GetCurrentProcess().Kill();
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message.ToString() + ex.StackTrace.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        flag = false;
        //    }
        //    return flag;
        //}

        //#endregion 加密狗验证

        //#region MES日志

        ////private delegate void AddMesRecvToText(string message, Color color);

        ////public static RichTextBox RichTextBox_Mes;

        ////public static void AppendMesInfoToRichTextBoxLog(string message, Color color)
        ////{
        ////    try
        ////    {
        ////        if (RichTextBox_Mes.InvokeRequired)
        ////        {
        ////            AddMesRecvToText cb = new AddMesRecvToText(AppendMesInfoToRichTextBoxLog);
        ////            RichTextBox_Mes.Invoke(new Action<RichTextBox, string, Color>(ShowTextBoxValue), RichTextBox_Mes, message.ToString(), color);
        ////        }
        ////        else
        ////        {
        ////            ShowTextBoxValue(RichTextBox_Mes, message, color);
        ////        }
        ////    }
        ////    catch (Exception ex)
        ////    {
        ////        Logmsg("方法:public void AppendToRichTextBoxLog(string message, Color color)错误，错误信息:\"" + new Exception(ex.Message.ToString()));
        ////    }
        ////}

        ////private static void ShowTextBoxValue(RichTextBox txt, string message, Color color)
        ////{
        ////    try
        ////    {
        ////        string str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + message + "\r\n";
        ////        txt.SelectionColor = color;
        ////        txt.AppendText(str);
        ////        txt.SelectionStart = txt.Text.Length;
        ////        txt.ScrollToCaret();
        ////        Logmsg(str);
        ////    }
        ////    catch (Exception ex)
        ////    {
        ////        Logmsg("方法:private void ShowTextBoxValue(RichTextBox txt, string message, Color color)错误，错误信息:" + new Exception(ex.Message.ToString()));
        ////    }
        ////}

        ////private static string LogPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "MesLog" + "\\";

        /////// <summary>
        /////// 创建日志
        /////// </summary>
        /////// <param name="path">日志名</param>
        /////// <param name="Log">信息</param>
        ////private static void Logmsg(string Log)
        ////{
        ////    try
        ////    {
        ////        string path = "/" + "System_MesLog_" + DateTime.Today.ToString("yyyyMMdd") + ".txt";

        ////        if (Directory.Exists(LogPath) == false)
        ////        {
        ////            Directory.CreateDirectory(LogPath);
        ////        }

        ////        if (!System.IO.File.Exists(LogPath + path))
        ////        {
        ////            using (FileStream fs = new FileStream(LogPath + path, FileMode.Create, FileAccess.Write))
        ////            {
        ////                using (StreamWriter sw = new StreamWriter(fs))
        ////                {
        ////                    sw.WriteLine(Log);
        ////                    sw.Close();
        ////                    fs.Close();
        ////                }
        ////            }
        ////        }
        ////        else
        ////        {
        ////            using (FileStream fs1 = new FileStream(LogPath + path, FileMode.Append, FileAccess.Write))
        ////            {
        ////                using (StreamWriter sw1 = new StreamWriter(fs1))
        ////                {
        ////                    sw1.WriteLine(Log);
        ////                    sw1.Close();
        ////                    fs1.Close();
        ////                }
        ////            }
        ////        }
        ////    }
        ////    catch (Exception ex)
        ////    {
        ////        MessageBox.Show("文本保存出错!", "错误提示" + ex.ToString());
        ////    }
        ////}

        //#endregion MES日志

        //public static MachineInfo HeartBeatMache = new MachineInfo();
    }
}